package com.wkck.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wkck.domain.BoxInfo;
import com.wkck.domain.User;
import com.wkck.domain.UserSendResume;
import com.wkck.service.JobService;
import com.wkck.service.NewsService;
import com.wkck.service.UserService;
import com.wkck.utils.Code;
import com.wkck.utils.Msg;
import com.wkck.utils.Result;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private JobService jobService;

    @Resource
    private NewsService newsService;

    /**
     * 根据username的信息得到该所有信息
     * @param username
     * @return
     */

    @GetMapping("/{username}")
//    @Cacheable(value = "UserMsgByUserName")
    public Result getUserMsgByUserName(@PathVariable String username){
        User user = userService.getUserMsgByUserName(username.trim());
        return new Result(Code.SELECT_OK,user, Msg.SELECT_OK);
    }

    /**
     * 个人中心 -> 修改用户信息
     * @param user
     * @return
     */

    @PostMapping("/updateUserMsg")
    public Result updateUserMsg(@RequestBody User user){
        return new Result(Code.UPDATE_OK,userService.updateById(user),Msg.UPDATE_OK);
    }

    @PutMapping("/personalUpdateUsePwd")
    public Result updateUserPwd(String username , String password){
        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getPassword,password)
                .eq(User::getUsername,username);

        boolean update = userService.update(luw);
        Integer code = update ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = update ? Msg.UPDATE_OK : Msg.UPDATE_ERR;
        return new Result(code,update,msg);
    }

    @GetMapping("/getUserSendResumeMsgById/{id}")
//    @Cacheable(value = "getUserSendResumeMsgById",key = "#id")
    public Result getUserSendResumeMsgById(@PathVariable Integer id){
        List<UserSendResume> userSendResumeMsg = userService.getUserSendResumeMsg(id);
        return new Result(Code.SELECT_OK,userSendResumeMsg,Msg.SELECT_OK);
    }

    /**
     * 返回系统已有用户人数
     * @return
     */

    @GetMapping
    @Cacheable(value = "getUserCount")
    public Result getUserCount(){

        HashMap<String,Integer> dishBoardInfo = new HashMap<>();
        dishBoardInfo.put("userCount",userService.count());
        dishBoardInfo.put("jobCount",jobService.count());
        dishBoardInfo.put("jobJoinCount",jobService.job_joinCount());
        dishBoardInfo.put("newsCount",newsService.count());

        return new Result(200,dishBoardInfo,"成功");
    }

    @GetMapping("/jobStatus")
    @Cacheable(value = "getAllUserStatus")
    public Result getAllUserStatus(){
        List<User> userList = userService.list();

        HashMap<String,Integer> userJobStatus = new HashMap<>();
        userList.forEach(item -> {
            if (userJobStatus.containsKey(item.getWork_status())) {
                userJobStatus.put(item.getWork_status(),userJobStatus.get(item.getWork_status())+1);
            } else {
                userJobStatus.put(item.getWork_status(),1);
            }
        });

        List<BoxInfo> statusList = new ArrayList<>();
        for (Map.Entry<String, Integer> next : userJobStatus.entrySet()) {
            statusList.add(new BoxInfo(next.getKey(), ((double)next.getValue() / userList.size()) * 100));
        }

        return new Result(Code.SELECT_OK,statusList,Msg.SELECT_OK);
    }
}
